﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TranslitMe
{
	public partial class Form1 : Form
	{
		private TranslitMe Transliter;
		private string _path = null;
		private string _destinationPath = null;

		public Form1()
		{
			Transliter = new TranslitMe();

			Transliter.Subdirectories=true;
			Transliter.CopyDirectoriesTree=true;
			Transliter.Operation=TranslitMe.OperationType.Move;
			Transliter.WhenExists = TranslitMe.WhenExistsAction.Nothing;
			Transliter.FileRenamed += _onCompleteFile;
			InitializeComponent();
		}

		private void buttonSelectPath_Click(object sender, EventArgs e)
		{
			if (folderBrowserDialogMain.ShowDialog() == DialogResult.OK)
			{
				_path = labelPath.Text = folderBrowserDialogMain.SelectedPath;
				if (checkBox2.Checked)
				{
					Transliter.DestinationFolder = "";
					labelDestinationPath.Text = labelPath.Text;
					buttonRun.Enabled = true;
				}
			}
		}

		private void buttonSelectDestination_Click(object sender, EventArgs e)
		{
			if (folderBrowserDialogDestination.ShowDialog() == DialogResult.OK)
			{
				Transliter.DestinationFolder = labelDestinationPath.Text = folderBrowserDialogDestination.SelectedPath;
				checkBox2.Checked = false;
			}
		}

		private void checkBox2_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox checkBox = (CheckBox) sender;
			if (checkBox.Checked)
			{
				Transliter.DestinationFolder = "";
				labelDestinationPath.Text = labelPath.Text;
			}
		}

		private void checkBoxSubdir_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.Subdirectories = ((CheckBox) sender).Checked;
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.CopyDirectoriesTree = ((CheckBox) sender).Checked;
		}

		private void radioButtonMove_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.Operation = TranslitMe.OperationType.Move;
		}

		private void radioButtonCopy_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.Operation = TranslitMe.OperationType.Copy;
		}

		private void radioButtonSymlink_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.Operation = TranslitMe.OperationType.SymbolicLink;
		}

		private void radioButtonSkip_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.WhenExists = TranslitMe.WhenExistsAction.Nothing;
		}

		private void radioButtonOverwrite_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.WhenExists = TranslitMe.WhenExistsAction.Rewrite;
		}

		private void radioButtonRename_CheckedChanged(object sender, EventArgs e)
		{
			Transliter.WhenExists = TranslitMe.WhenExistsAction.Rename;
		}

		private void buttonRun_Click(object sender, EventArgs e)
		{
			_encodeFilenames();
		}

		//TODO: в отдельный поток
		private void _encodeFilenames()
		{
			System.IO.SearchOption sOprion = System.IO.SearchOption.TopDirectoryOnly;
			if (Transliter.Subdirectories)
			{
				sOprion = System.IO.SearchOption.AllDirectories;
			}
			progressBar.Maximum = System.IO.Directory.GetFiles(_path, "*", sOprion).Length;
			Transliter.RenameFiles(_path);
		}

		//TODO: событие при переименовавынии
		private void _onCompleteFile(object sender, TranslitMe.ResultArgs args)
		{

		}
	}
}
